package com.lse_from.utils

import com.google.gson.Gson
import com.google.gson.reflect.TypeToken

/**
 * 字段工具
 *
 * @author 祁雪
 */
class FieldUtil {

    companion object {

        /**
         * 检查字段 返回字段列表
         */
        fun checkFieldHav(field: String, checkField: Boolean): List<Map<String, Any?>> {
            val rows: List<List<List<Map<String, Any>>>>
            val gson = Gson()
            try {

                rows = gson.fromJson(
                    field,
                    object : TypeToken<List<List<List<Map<String, Any>>>>>() {}.type
                ) as List<List<List<Map<String, Any>>>>

            } catch (e: Exception) {
                throw RuntimeException("数据格式不正确")
            }
            var list = arrayListOf<Map<String, Any?>>()

            rows.forEach { cols ->
                cols.forEach { eles ->
                    eles.forEach {
                        var config: Map<String, Any>? = null
                        if (it.get("config") != null) {
                            config = gson.fromJson(
                                gson.toJson(it.get("config")),
                                object : TypeToken<Map<String, Any>>() {}.type
                            ) as Map<String, Any>
                        }
                        list.add(
                            hashMapOf(
                                "field" to if (config != null) {
                                    config.getOrDefault("field", null)
                                } else null,
                                "default" to if (config != null) {
                                    config.getOrDefault("default", null)
                                } else null,
                                "tableLabel" to if (config != null) {
                                    config.getOrDefault("tableLabel", null)
                                } else null,
                                "type" to it.getOrDefault("type", "String")
                            )
                        )
                    }
                }
            }

            list = list.filter { it.get("field") != null } as ArrayList<Map<String, Any?>>

            if (checkField) {
                list.forEach {
                    checkField(list, it)
                }
            }

            return list
        }

        private fun checkField(eles: List<Map<String, Any?>>, ele: Map<String, Any?>) {

            if (eles.isNullOrEmpty()) {
                return
            }

            val filedName = "field"
            // 检测是否存在未设置字段的组件
            if (ele.get(filedName) != null && ele.get(filedName).toString().isEmpty()
            ) {
                throw RuntimeException("检测到有未设置字段的组件")
            }
            // 检测重复字段
            val filterEles = eles.filter {
                return@filter (it.get(filedName) != null
                        && it.get(filedName).toString().equals(ele.get(filedName).toString())
                        )
            }

            if (filterEles.size > 1) {
                throw RuntimeException("检测到有字段冲突")
            }

        }

    }

}